package com.maishi.programmer.jainzhioffer;

import java.util.ArrayList;
import java.util.Stack;

/**
 * Created by brossdong on 2018/5/5.
 * 从尾到头打印链表
 */
public class TestProblem6 {

    /**
     * 输入一个链表，从尾到头打印链表每个节点的值。
     */
    public class ListNode {
        int val;
        ListNode next = null;

        ListNode (int val) {
            this.val = val;
        }
    }


    public static ArrayList<Integer> printListFromTailToHead(ListNode node) {
        Stack<Integer> stack = new Stack<>();
        ArrayList<Integer> list = new ArrayList<Integer>();
        if (node == null) return list;
        ListNode temp = node;
        while (temp != null) {
            stack.push(temp.val);
            temp = temp.next;
        }
        while (!stack.empty()) {
            list.add(stack.pop());
        }
        return list;
    }

    public static void main(String[] args) {

    }

}
